package dao.imp;

import dao.It_userDAO;
import domain.JDBCUntis;
import domain.t_user;

import java.sql.*;
import java.util.ArrayList;

public class t_userimp implements It_userDAO {
  Connection conn = null;
  Statement st = null;
  ResultSet rs = null;
  PreparedStatement pst = null;

  @Override
  // 添加
  public int insert(domain.t_user user) {
    int row = 0;

    try {
      conn = JDBCUntis.getconnection();
      conn.setAutoCommit(false);
      st = conn.createStatement();
      String sql =
          "INSERT INTO t_user(name,sex)"
              + "values ('"
              + user.getName()
              + "','"
              + user.getSex()
              + "');";
      row = st.executeUpdate(sql);
      conn.commit();
    } catch (Exception e) {
      e.printStackTrace();
      conn.rollback();
    } finally {
      JDBCUntis.closet(conn, st, null);
      return row;
    }
  }

  @Override
  //删除
  public int delect(int id) {
    int row=0;
    conn=JDBCUntis.getconnection();

    String sql="delete from t_user where id="+id;
    try {
      conn.setAutoCommit(false);
      pst=conn.prepareStatement(sql);
      row=pst.executeUpdate();
      conn.commit();
    } catch (SQLException throwables) {
      throwables.printStackTrace();
      try {
        conn.rollback();
      } catch (SQLException e) {
        e.printStackTrace();
      }
    }
    return row;
  }

  @Override
  // 修改
  public int update(domain.t_user user, int i) {
int row=0;
    conn = JDBCUntis.getconnection();

    String sql = "update  t_user  set name=?,sex=? where id="+i;

    try{
      conn.setAutoCommit(false);
    pst = conn.prepareStatement(sql);
    pst.setString(1, user.getName());
    pst.setString(2, user.getSex());
    row=pst.executeUpdate();
    conn.commit();
}catch (Exception e){
      e.printStackTrace();
    }finally{
      JDBCUntis.closet(conn,pst,null);
      try {
        conn.rollback();
      } catch (SQLException throwables) {
        throwables.printStackTrace();
      }
    }
    return row;
  }

  @Override
  // 获取单个
  public domain.t_user select(int id) {
    t_user user = new t_user();

    try {
      conn = JDBCUntis.getconnection();
      st = conn.createStatement();
      String sql = "select * from t_user where id=" + id;
      rs = st.executeQuery(sql);
      while (rs.next()) {
        user.setId(rs.getInt(1));
        user.setName(rs.getString(2));
        user.setSex(rs.getString(3));
        System.out.print(rs.getInt(1) + "  ");
        System.out.print(rs.getString(2) + "  ");
        System.out.print(rs.getString(3) + "  ");
      }
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      JDBCUntis.closet(conn, st, rs);
    }

    return user;
  }
  // 获取全部
  public ArrayList<t_user> Listselect() {
    ArrayList<t_user> list = new ArrayList();

    try {
      conn = JDBCUntis.getconnection();
      st = conn.createStatement();
      String sql = "select * from t_user ";
      rs = st.executeQuery(sql);
      while (rs.next()) {
        list.add(new t_user(rs.getInt(1), rs.getString(2), rs.getString(3)));
      }
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      JDBCUntis.closet(conn, st, rs);
    }

    return list;
  }
}
